package com.zhangyu.shadow.repair.delegate.maintain;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.zhangyu.shadow.repair.R;
import com.zhangyu.shadow.repair.delegate.bean.RepairTypeBean;

import java.util.List;

import st.lib.base.util.utilcode.util.ToastUtils;

/**
 * @author zhangyu
 * @date 2018/12/6
 */

public class RepairTypeDialog extends Dialog {
    private OnConfirmListener mConfirmListener;
    private RepairTypeBean mRepairTypeBean;
    private RadioGroup mRadioGroup;
    private Context mContext;

    public RepairTypeDialog(@NonNull Context context,
                            List<RepairTypeBean> repairTypeBeans,
                            OnConfirmListener listener) {
        super(context);
        mConfirmListener = listener;
        mContext = context;
        setTitle("请选择小修类型");
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_multi_person, null);
        mRadioGroup = view.findViewById(R.id.radio_group);

        view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hide();
                if(mConfirmListener != null){
                    mConfirmListener.onHide();
                }
            }
        });
        view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mRepairTypeBean == null) {
                    ToastUtils.showShortSafe("请选择小修类型");
                } else {
                    if (mConfirmListener != null) {
                        mConfirmListener.onConfirm(mRepairTypeBean);
                    }
                    hide();
                }
            }
        });
        setContentView(view);
        setRadioGroup(repairTypeBeans);
    }

    public void setRadioGroup(List<RepairTypeBean> repairTypeBeans) {
        mRadioGroup.removeAllViews();
        for (final RepairTypeBean repairTypeBean : repairTypeBeans) {
            RadioButton radioButton = new RadioButton(mContext);
            radioButton.setId(Integer.parseInt(repairTypeBean.getId()));
            radioButton.setPadding(10, 10, 10, 10);
            radioButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            radioButton.setText( repairTypeBean.getName());
            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mRepairTypeBean = repairTypeBean;
                }
            });
            RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(
                    RadioGroup.LayoutParams.WRAP_CONTENT,
                    RadioGroup.LayoutParams.WRAP_CONTENT);
            lp.setMargins(15, 0, 0, 0);
            mRadioGroup.addView(radioButton, lp);
        }
    }

    public interface OnConfirmListener {
        public void onConfirm(RepairTypeBean repairTypeBean);
        public void onHide();
    }
}
